/*#######################################################################*/
/* Copyright [2011-2012] Delta Air Lines, Inc./Delta Technology, Inc.    */
/*                       All Rights Reserved                             */
/*              Access, Modification, or Use Prohibited                  */
/* Without Express Permission of Delta Air Lines or Delta Technology.    */
/* ######################################################################*/
package com.delta.rafg.core.log.impl;

import java.io.File;
import java.io.FileNotFoundException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * <code>AxisLogger</code> implements the Log4J features of logging it to a
 * file specified.
 * 
 * @author 737061
 */
final class AxisLogger {

				public static final String SYSTEM_RAFG_CONFIG_PATH_KEY = "com.delta.rafg.core.config.path";
				
				private static Logger LOGGER = Logger.getLogger(AxisLogger.class);
				
				private static long ONE_MINUTE_INMILLIS = 60000L;

				static {
					
			        try {
			        	//first load the properties from the file system
			        	System.out.println("AxisLogger loading config from properties file");
			        	loadPropsFromFile();
			        } 
			        catch (Throwable t) {
			        	//if that does not work then use the file from ear
			        	System.out.println("AxisLogger loading config from properties in EAR file because \n" + t);
			        	loadDefaultProps();
			        }
				}
				
				private static void loadPropsFromFile() throws Exception {
		        	String localConfigPath = System.getProperty(SYSTEM_RAFG_CONFIG_PATH_KEY);

		        	if (localConfigPath != null && localConfigPath.length() >= 0) {
		        		String filePath = localConfigPath + "/" + "log4j.properties";
		        		File theLogSettings = new File(filePath);
		        		if(theLogSettings.exists()){
		        			PropertyConfigurator.configureAndWatch(theLogSettings.getCanonicalPath(), 15 * ONE_MINUTE_INMILLIS);
		        		}
		        		else{
		        			throw new FileNotFoundException(theLogSettings.getCanonicalPath());
		        		}
		        	}
		        	else{
		        		throw new Exception("System Property Not Found: " + SYSTEM_RAFG_CONFIG_PATH_KEY);
		        	}
				}
				
				private static void loadDefaultProps(){
					if("Production".equalsIgnoreCase(getMyEnvirontment()) 
							|| "SystemTest".equalsIgnoreCase(getMyEnvirontment())
							|| "SystemIntegration".equalsIgnoreCase(getMyEnvirontment())){
						PropertyConfigurator.configure(AxisLogger.class
							    .getResource("log4j.properties"));
					}
					else if("Dev".equalsIgnoreCase(getMyEnvirontment()) 
							|| getMyEnvirontment().trim().length() <=0 ){
						PropertyConfigurator.configure(AxisLogger.class
							    .getResource("log4j.Dev.properties"));
					}
					else{
						PropertyConfigurator.configure(AxisLogger.class
							    .getResource("log4j.properties"));
					}
					
				}
			    
				private static String myEnvironment = null; 
				private static String getMyEnvirontment(){
					if(myEnvironment == null){
						String environment = System.getProperty("com.delta.rafg.core.config.environment");
						myEnvironment = (environment == null) ? "" : environment;
					}
					return myEnvironment;
				}				

				/**
				 * Prints out a debug message to the file.
				 * 
				 * @param message
				 */
				public static void debug(Object message) {
								if (LOGGER.isDebugEnabled()) {
												LOGGER.debug(message);
								}
				}

				/**
				 * Prints out an error message to the file with no exception.
				 * 
				 * @param message
				 */
				public static void error(Object message) {

								error(message, null);
				}

				/**
				 * Prints out an error message to the file with exception.
				 * 
				 * @param message
				 */
				public static void error(Object message, Throwable exception) {

								LOGGER.error(message, exception);
				}

				/**
				 * Prints out an information message.
				 * 
				 * @param message
				 */
				public static void info(Object message) {

								LOGGER.info(message);
				}

				// static public void main(String[] args) {
				//
				// AxisLogger.info("Information Message");
				// AxisLogger.debug("Debug Message");
				// AxisLogger.warn("Warning Message");
				// AxisLogger.error("Error Message");
				// AxisLogger.error("Error with Exception", new Exception());
				// }

				/**
				 * Prints out an warning message to the file.
				 * 
				 * @param message
				 */
				public static void warn(Object message) {

								LOGGER.warn(message);
				}

				/**
				 *Constructor
				 */
				public AxisLogger(){

				}

}
